package framework.states;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.GameState;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;

import framework.BasicActionGetter;

public class BackableState extends BasicGameState implements Backable {
	
	protected boolean back;
	
	protected GameState previoustate;
	
	BasicActionGetter action;

	@Override
	public void init(GameContainer gc, StateBasedGame sg)
			throws SlickException {
		back = false;
		previoustate = sg.getState(Menu.ID);
	}

	@Override
	public void enter(GameContainer container, StateBasedGame game)
			throws SlickException {
		super.enter(container, game);
		back = false;
	}



	@Override
	public void render(GameContainer gc, StateBasedGame sg, Graphics g)
			throws SlickException {
	}

	@Override
	public void update(GameContainer g, StateBasedGame sg, int delta)
			throws SlickException {
		if (isBack() && getPreviousState() != null) {	
			sg.enterState(getPreviousState().getID(), new FadeOutTransition(), new FadeInTransition());
		}
		
	}

	public boolean isBack() {
		return back;
	}
	
	public void setIsBack(boolean back) {
		this.back = back;
	}

	public GameState getPreviousState() {
		return previoustate;
	}

	@Override
	public int getID() {
		return 12;
	}
	
	@Override
	public void keyPressed(int key, char c) {
		setIsBack(key == Input.KEY_ESCAPE);
	}
	
	@Override
	public void controllerButtonPressed(int controller, int button) {
		setIsBack(button == 2);
	}

	@Override
	public void setPreviousState(GameState state) {
		this.previoustate = state;
		
	}
}
